%% ex1.m
% set dir: src/
close all;
clear; clc;

%% test function
img = imread("../data/lunar_surface.tif");
FIMG = dft2D(img); % conduct fft
F0 = fftshift(FIMG); % rearrange fft output by shifting the zero-frequency component to the center of the spectrum. 
S = log(abs(F0) + 1); % in case of element in abs(F0) is 0
invFFT = ifft2(FIMG);
figure;
subplot(1,3,1);
imshow(img,[0,255]);
title('Original Image')
axis off;
subplot(1,3,2);
imshow(S,[]);
title('Spectrogram');
axis off;
subplot(1,3,3)
imshow(real(invFFT),[0,255]);
axis off;
title('Inverse Fourier Transform')
%% dft2D
function F = dft2D(f)
[m,n] = size(f);
F = zeros(m,n);
for i=1:m
    F(i,:) = fft(f(i,:));
end
for j=1:n
    F(:,j) = fft(F(:,j));
end
end